﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using CashDriver;
using CashCommon;
using Cash.Model;
using CashResource;

namespace Cash
{
    static class Program
    {
        /// <summary>
        /// Главная точка входа для приложения.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            
            //// Проверка обновлений
            //try
            //{
            //    new Updater().Update();
            //}
            //catch (Exception err)
            //{
            //    throw new ECash(MessageType.AllertLog, CommonError.UpdateError, err);
            //}

            Builder.Init();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Form formConnect = Builder.FormConnect;
            if (formConnect.ShowDialog() == DialogResult.OK)
            {
                Adjustment.AdjustmentFile = String.Format("{0}\\{1}\\{2}", Adjustment.AppDir, Builder.DB.Login, "Settings.adj");
                Adjustment.Load();
                CashEmployer.Inst.Init(Builder.DB.Login);
                Application.Run(new FormMain());
            }

        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            ECash.HandleError((Exception)e.ExceptionObject);
        }

        static void OnThreadException(object sender, ThreadExceptionEventArgs t)
        {
            ECash.HandleError((Exception)t.Exception);
        }
        
    }
}
